import java.util.LinkedList;

public class PassportUnit extends Thread {
	int id;
	int waitTime;
	LinkedList<Passenger> queue = new LinkedList<Passenger>();
	LinkedList<PassportUnit> PUList = new LinkedList<PassportUnit>();

	public PassportUnit(int id, int waitTime) {
		this.id = id;
		this.waitTime = waitTime;

	}

	public void run() {
		for (int i = 0; i < Launch.PassportList.size(); i++) {
			PUList.add(Launch.PassportList.get(i));

		}

		while (true) {

			if (PUList.get(this.id - 1).queue.size() == 0) {
				try {
					synchronized (this) {

						this.wait(1);

					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			
			if(this.queue.size()>0)
			try {
				Passenger p;
				synchronized (this) {
					p = this.queue.remove(0);
					Launch.textArea1.get(this.id-1).validate();
					synchronized (p) {
						p.wait(500);

					}

					for (int i = 0; i < Launch.CarouselList.size(); i++) {
						if (Launch.CarouselList.get(i).busy && Launch.CarouselList.get(i).flightName.equalsIgnoreCase(p.flight.Inbound)) {
							BaggageCarousel bg = Launch.CarouselList.get(i);
							bg.passengerList.add(p);
						

							synchronized (this) {
								if (!bg.isAlive())
									bg.notify();

							}
							
							bg.update(Launch.CarouselList.get(i).passengerList.size());
							
							
							
							break;
						}
					}
					
					Launch.textArea1.get(this.id-1).setText("Queue Length : " + this.queue.size()+"\n"+"Waiting Time : " + this.queue.size()*this.waitTime/1000 + " Minutes");

					Launch.textArea1.get(this.id-1).validate();
					this.wait(waitTime);

				}
				synchronized (this) {

					this.notify();

				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}

	}

}
